﻿using System;
using System.Collections;
using System.Text;
using System.Web.Mvc;
using System.Web.UI;

namespace System.Web.Mvc
{
    public static class AjaxHelperExtensions
    {
        public static string RegisterJqueryMvc(this AjaxHelper helper, ClientScriptManager scriptManager)
        {
            //TODO: find out how to do this without a scriptmanager, those damned classes are all sealed & internal
            return string.Format("<script src=\"{0}\" type=\"text/javascript\"></script>\n<script src=\"{1}\" type=\"text/javascript\"></script>\n<script src=\"{2}\" type=\"text/javascript\"></script>",
                scriptManager.GetWebResourceUrl(typeof(JqueryMvc.JqueryMvcApplication), "JqueryMvc.ClientScripts.jquery-1.2.3.min.js"),
                scriptManager.GetWebResourceUrl(typeof(JqueryMvc.JqueryMvcApplication), "JqueryMvc.ClientScripts.jquery.history_remote.pack.js"),
                scriptManager.GetWebResourceUrl(typeof(JqueryMvc.JqueryMvcApplication), "JqueryMvc.ClientScripts.mvc.js"));
        }
        public static string ActionLink(this AjaxHelper helper, string linkText, string actionName, string target)
        {
            return ActionLink(helper, linkText, actionName, null, target);
        }
        public static string ActionLink(this AjaxHelper helper, string linkText, string actionName, string controllerName, string target)
        {
            HtmlHelper htmlHelper = new HtmlHelper(helper.ViewContext);
            string link = controllerName == null ? htmlHelper.ActionLink(linkText, actionName) :
                htmlHelper.ActionLink(linkText, actionName, controllerName);

            if (target == null)
                return link;
            else if (!target.StartsWith("#"))
                target = "#" + target;

            return link.Replace("<a", "<a class=\"ajaxlink\" meta:ajaxtarget=\"" + target + "\"");
        }
    }
}
